import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { CreateTableWizardWindow } from "../customWindows/createTableWizard";
import { tableBaseConfigMap, CreateTableParams } from "@/mdesign-lib/mdesign-app.umd.js";
import { archService } from "@/mdesign-lib/mdesign-app.umd.js";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";

export class CreateTableWizardAction extends Action {
  keyName = "createTableWizard" as ActionKey;
  desc = "createTableWizard";

  run(projectId: string, option: any) {
    const sts = option.appliedStereotypes[0];
    const createTableParams = tableBaseConfigMap.get(sts)!.createTableParams as CreateTableParams;
    const defaultOwnerId = option.ownerId;
    CreateTableWizardWindow.popup(
      {
        projectId,
        stereotype: sts,
        defaultTableName: option.defaultTableName,
        stepKeys: createTableParams.stepKeys,
        stepParams: JSON.stringify(createTableParams.stepParams || "{}"),
        disableStepKey: createTableParams.disableStepKey,
        defaultOwnerId
      },
      async ({ success, result }) => {
        if (!success) return;
        console.log("result", result);
        const { name, ownerId, defaultRows, defaultColumns, startDate, endDate, dateModelId, timeLineId } = result;
        // await modelService.createForecastTable({
        //   projectId,
        //   defaultName: name,
        //   parentId: ownerId || option.ownerId,
        //   applyStereotypes: option.appliedStereotypes,
        //   metaclass: option.metaclass,
        //   defaultRows,
        //   defaultColumns
        // });
        await archService
          .createTableWizard({
            projectId,
            defaultName: name,
            parentId: ownerId || option.ownerId,
            applyStereotypes: option.appliedStereotypes,
            metaclass: option.metaclass,
            defaultRows,
            defaultColumns,
            timeLineId,
            startDate,
            endDate,
            headerId: dateModelId?.[0]
          })
          .finally(() => CreateTableWizardWindow.done());
      }
    );
  }
}
